<!--富文本编辑器-->
<template>
    <div class="rich-text-editor-container" v-loading="loading">
        <quill-editor   v-model="content" :options="editorOptions" class="ql-editor" ref="myQuillEditor"
            @change="onEditorChange($event)">
        </quill-editor>
        <!--视频上传弹窗-->
        <div>
            <el-dialog :close-on-click-modal="false" width="50%" style="margin-top: 1px" title="视频上传"
                :visible.sync="videoForm.show" append-to-body>
                <el-tabs v-model="videoForm.activeName">
                    <el-tab-pane label="添加视频链接" name="first">
                        <el-input v-model="videoForm.videoLink" placeholder="请输入视频链接" clearable></el-input>
                        <el-button type="primary" size="small" style="margin: 20px 0px 0px 0px "
                            @click="insertVideoLink(videoForm.videoLink)">确认
                        </el-button>
                    </el-tab-pane>
                    <el-tab-pane label="本地视频上传" name="second">
                        <el-upload v-loading="loading" style="text-align: center;" drag
                            :action="uploadVideoConfig.uploadUrl" accept="video/*" :name="uploadVideoConfig.name"
                            :before-upload="onBeforeUploadVideo" :on-success="onSuccessVideo" :on-error="onErrorVideo"
                            :multiple="false">
                            <i class="el-icon-upload"></i>
                            <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
                            <div class="el-upload__tip" slot="tip">只能上传MP4文件，且不超过{{uploadVideoConfig.maxSize}}M</div>
                        </el-upload>
                    </el-tab-pane>
                </el-tabs>
            </el-dialog>
        </div>
    </div>
</template>
<script>
    // require styles
    import 'quill/dist/quill.core.css'
    import 'quill/dist/quill.snow.css'
    import 'quill/dist/quill.bubble.css'
    import {
        quillEditor
    } from 'vue-quill-editor'
    // 工具栏
    const toolbarOptions = [
        ["bold", "italic", "underline", "strike"], // 加粗 斜体 下划线 删除线
        ["blockquote", "code-block"], // 引用  代码块
        [{
            list: "ordered"
        }, {
            list: "bullet"
        }], // 有序、无序列表
        [{
            indent: "-1"
        }, {
            indent: "+1"
        }], // 缩进
        [{
            size: ["small", false, "large", "huge"]
        }], // 字体大小
        [{
            header: [1, 2, 3, 4, 5, 6, false]
        }], // 标题
        [{
            color: []
        }, {
            background: []
        }], // 字体颜色、字体背景颜色
        [{
            align: []
        }], // 对齐方式
        ["clean"], // 清除文本格式
        ['image','video'] // 视频
    ]
    export default {
        name: 'RichTextEditor',
        components: {
            quillEditor
        },
        props: {
            /* 编辑器的内容 */
            content: { // 返回的html片段
                type: String,
                default: "<p>11</p>"
            },
            quill_info,
            // 视频上传配置
            uploadVideoConfig: {
                type: Object,
                default () {
                    return {
                        uploadUrl: '', // 上传地址
                        maxSize: 100, // 图片上传大小限制，默认不超过2M
                        name: 'Filedata' // 图片上传字段
                    }
                }
            }
        },
        data() {
            let _self = this;
            return {
                loading: false, // 加载loading
                content:'<p>11</p>',
                editorOptions: {
                    placeholder: '',
                    theme: 'snow', // or 'bubble'
                    modules: {
                        toolbar: {
                            container: toolbarOptions, // 工具栏
                            handlers: {
                                'video': () => {
                                    // 覆盖默认的上传视频，点击视频图标，显示弹窗
                                    _self.videoForm.show = true
                                }
                            }
                        }
                    }
                },
                // 视频上传变量
                videoForm: {
                    show: false, // 显示插入视频链接弹框的标识
                    videoLink: '',
                    activeName: 'first'
                }
            }
        },
        mounted() {
           console.log('88888888888')
        },
       
        
        methods: {
               
        
            // 文本编辑
            onEditorChange({
                quill,
                html,
                text
            }) {
                // console.log('editor changed:', quill, html, text)
                console.log('editor changed:',html)
                console.log('我是父组件传进来的',quill_info)
                // console.log('html.replace(/<[^>]*>|/g:', html.replace(/<[^>]*>|/g))
                // this.$emit('update:content', html)
                // this.$emit('change', html)
                this.$emit('test', html)
            },
            hideLoading() {
                this.loading = false
            },
            /** --------- 视频上传相关 start ---------  */
            insertVideoLink(videoLink) {
                console.log('videoLink:','cao')
                if (!videoLink) return this.$message.error('视频地址不能为空！')
                this.videoForm.show = false
                let quill = this.$refs['myQuillEditor'].quill
                // 获取富文本
                let range = quill.getSelection()
                // 获取光标位置：当编辑器中没有输入文本时，这里获取到的 range 为 null
                let index = range ? range.index : 0
                // 在光标所在位置 插入视频
                quill.insertEmbed(index, 'video', videoLink)
                // 调整光标到最后
                quill.setSelection(index + 1)
            },
            // el-文件上传组件
            onBeforeUploadVideo(file) {
                this.loading = true
                let acceptArr = ['video/mp4']
                const isVideo = acceptArr.includes(file.type)
                const isLt1M = file.size / 1024 / 1024 < this.uploadVideoConfig.maxSize
                if (!isVideo) {
                    this.hideLoading()
                    this.$message.error('只能上传mp4格式文件!')
                }
                if (!isLt1M) {
                    this.hideLoading()
                    this.$message.error(`上传文件大小不能超过 ${this.uploadVideoConfig.maxSize}MB!`)
                }
                return isLt1M && isVideo
            },
            // 文件上传成功时的钩子
            onSuccessVideo(res) {
                this.hideLoading()
                if (res.code === '100') {
                    this.insertVideoLink(res.url)
                } else {
                    this.$message.error(res.desc)
                }
            },
            // 文件上传失败时的钩子
            onErrorVideo() {
                this.hideLoading()
                this.$message.error('上传失败')
            },
            /**--------- 视频上传相关 end --------- */
        }
    }
</script>


<style lang="scss" scoped>
    .rich-text-editor-container .ql-container {
        height: 300px;
    }

    .rich-text-editor-container .ql-editor {
        padding: 0;
    }

    .rich-text-editor-container .ql-tooltip {
        left: 5px !important;
    }
</style>